package com.gin.no_gay_alliance.viewmodel;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.response.body.ReadBody;
import com.gin.nga.response.field.CustomLevel;
import com.gin.nga.response.field.TopicDetail;
import com.gin.nga.response.field.user.UserContext;
import com.gin.nga.utils.AnonymousUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 回复列表数据
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/14 14:47
 **/
public class ReadItem {
    @JsonIgnore
    public List<CustomLevel> customLevels;
    /**
     * 当前页码
     */
    public Integer page = 1;
    /**
     * 楼层总数(含主楼)
     */
    public Integer total;
    /**
     * 总页数
     */
    public Integer totalPage;
    /**
     * 每页回复数
     */
    public Integer size = 20;
    /**
     * 楼主用户id
     */
    public String authorUid;
    /**
     * 回复数据
     */
    public List<ReplyItem> replyItems;
    /**
     * 版面ID
     */
    public Long forumId;

    public Long topicId;
    public String title;
    public TopicItem topicItem;

    public ReadItem() {
    }

    public ReadItem(ReadBody body) {
        if (body.getForum() != null) {
            this.customLevels = body.getForum().getCustomLevels();
            this.forumId = body.getForum().getForumId();
        }
        if (body.getTopicInfo() != null) {
            this.topicItem = new TopicItem(body.getTopicInfo());
            if (AnonymousUtils.isAnonymous(this.topicItem.authorName)) {
                this.topicItem.authorId = this.topicItem.authorName;
            }
        }
        this.page = body.getPage();
        this.totalPage = body.getTotalPage();
        this.size = body.getSize();
        this.total = body.getTotal();

        TopicDetail topicInfo = body.getTopicInfo();
        if (topicInfo != null) {
            this.authorUid = topicInfo.getAuthorUid();
            this.forumId = topicInfo.getForumId();
            this.topicId = topicInfo.getTopicId();
            this.title = topicInfo.getTitle();
        }

        UserContext userContext = body.getUserContext();
        this.replyItems = body.getReplies().values().stream().map(i -> {
            ReplyItem replyItem = new ReplyItem(i);
            replyItem.forumId = forumId;
            replyItem.setUserInfo(userContext.getUserInfo(replyItem.authorUid, this.customLevels));

            if (!ObjectUtils.isEmpty(replyItem.comment)) {
                replyItem.comment.forEach(c -> {
                    c.forumId = forumId;
                    c.setUserInfo(userContext.getUserInfo(c.authorUid, this.customLevels));
                    // 移除评论贴条的第一个节点
                    c.nodes.remove(0);
                });
            }
            if (!ObjectUtils.isEmpty(replyItem.hotReplies)) {
                replyItem.hotReplies.forEach(c -> {
                    c.forumId = forumId;
                    c.setUserInfo(userContext.getUserInfo(c.authorUid, this.customLevels));
                });
            }

            return replyItem;
        }).collect(Collectors.toList());
    }
}
